package cn.myzqu.start.lambda;

import cn.myzqu.start.lambda.entity.Employee;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupingBy {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "e1", 30, Employee.Rank.MANAGER, Employee.Gender.MALE));
        employees.add(new Employee(2, "e2", 31, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
        employees.add(new Employee(3, "e3", 29, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
        employees.add(new Employee(4, "e4", 24, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
        employees.add(new Employee(5, "e5", 25, Employee.Rank.PROGRAMMER, Employee.Gender.FEMALE));
        employees.add(new Employee(6, "e6", 29, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
        employees.add(new Employee(7, "e7", 25, Employee.Rank.TESTER, Employee.Gender.FEMALE));
        employees.add(new Employee(8, "e8", 27, Employee.Rank.TESTER, Employee.Gender.MALE));
        employees.add(new Employee(9, "e9", 28, Employee.Rank.OPERATOR, Employee.Gender.MALE));
        employees.add(new Employee(10, "e10", 26, Employee.Rank.OPERATOR, Employee.Gender.FEMALE));
        System.out.println("所有员工：" + employees);
        // 对员工按职能进行分类
        Map<Employee.Rank, List<Employee>> map = employees.stream()
                .collect(Collectors.groupingBy(Employee::getRank, Collectors.toList()));
        System.out.println("分组之后：" + map);
        // 对组内员工按年龄排序
        map.values().stream().forEach(GroupingBy::sortList);
        System.out.println("组内年龄排序之后：" + map);
    }
    // 对员工按年龄大小进行排序
    private static void sortList(List<Employee> list) {
        Collections.sort(list, (bean1, bean2) -> bean1.getAge().compareTo(bean2.getAge()));
    }
}
